<template>
  <div class="app-container">
    <div v-show="!modifyInfo.visible && !detailInfo.visible">
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <NewPageFilter
          v-model:query="filterInfo.query"
          :query-more-json="false"
          :filter-list="filterInfo.searchList"
          :filter-btn="filterInfo.searchBtn"
          :clear-type="clearType"
        >
          <template v-slot:filter-openTime="scope">
            <el-date-picker
              v-model="openTime"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开标开始时间"
              end-placeholder="开标结束时间"
              value-format="YYYY-MM-DD HH:mm"
              format="YYYY-MM-DD HH:mm"
              @change="timeChange"
              :shortcuts="$fn.pickerOptionsFn()"
              :default-time="[
                new Date(2000, 1, 1, 0, 0, 0),
                new Date(2000, 1, 1, 23, 59, 59),
              ]"
            >
            </el-date-picker>
          </template>
        </NewPageFilter>
      </div>
      <div class="filter-wrapper">
        <NewPageFilter :filter-btn="filterInfo.btnList"> </NewPageFilter>
      </div>
      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        v-model:query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :row-class-name="tableRowClassName"
      >
        <template v-slot:col-biddingNumber="scope">
          <span class="slip-edit" @click="showDetail(scope.row)">{{
            scope.row.biddingNumber
          }}</span>
        </template>
      </TableContainer>
    </div>

    <Modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      :orderId="modifyInfo.orderId"
      @returnModify="returnModify"
    />

    <Detail
      v-if="detailInfo.visible"
      v-model:visible="detailInfo.visible"
      :orderId="detailInfo.orderId"
      @returnBack="getList"
    />
    <Select-asset
      v-if="selectAssetInfo.visible"
      v-model:show-dialog="selectAssetInfo.visible"
      :highlightRow="false"
      :checkBox="true"
      :query="selectAssetInfo.query.queryMap"
      :checkedList="selectAssetInfo.checkedList"
      @callback="callBackAsset"
    />
  </div>
</template>

<script setup name="biddingProject">
import {
  getProjectPage,
  delProject,
  mappingBiddingProjectAssetMappingAsset,
  getMappingBiddingProjectAssetById,
} from '@/api/bidding'
import Modify from './modify'
import Detail from './detail'
import { onMounted } from 'vue'
const { proxy } = getCurrentInstance()
const openTime = ref([])
// 过滤相关配置
const filterInfo = ref({
  query: {},
  copyQuery: {},
  searchList: [
    {
      componentType: 'input',
      label: '招标编号',
      value: 'biddingNumberLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '项目名称',
      value: 'nameLike',
      event: search,
    },
    {
      slot: true,
      label: '开标时间',
      value: 'openTime',
      event: search,
    },
    {
      componentType: 'input',
      label: '开标地点',
      value: 'openAddressLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '采购人',
      value: 'purchUserLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '代理机构',
      value: 'purchAgencyLike',
      event: search,
    },
  ],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [
    {
      type: 'button',
      label: '新增',
      event: create,
      show: proxy.$permission('biddingProject:add'),
    },
  ],
})
const clearType = ref(true)
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  selections: [],
  fieldList: [
    {
      label: '招标编号',
      value: 'biddingNumber',
      type: 'slot',
    },
    { label: '项目名称', value: 'name' },
    { label: '预算金额', value: 'budgetAmount' },
    { label: '开标时间', value: 'openTime' },
    { label: '开标地点', value: 'openAddress' },
    { label: '采购人', value: 'purchUser' },
    { label: '采购联系人', value: 'purchContactUser' },
    { label: '采购联系人电话', value: 'purchContactUserTel' },
    { label: '采购代理机构', value: 'purchAgency' },
    { label: '代理机构联系人', value: 'agencyUser' },
    { label: '代理机构联系人电话', value: 'agencyUserTel' },
    { label: '备注', value: 'remark' },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '100',
    btList: [
      {
        label: '修改',
        event: update,
        show: proxy.$permission('biddingProject:update'),
      },
      {
        label: '删除',
        event: remove,
        popconfirmText: '删除后将改变数据,是否继续？',
        show: proxy.$permission('biddingProject:delete'),
      },
      {
        label: '关联资产卡片',
        event: mappingAsset,
        show: proxy.$permission('biddingProject:mappingAsset'),
      },
    ],
  },
})
const modifyInfo = ref({
  visible: false,
  orderId: '',
  title: '',
})

const detailInfo = ref({
  visible: false,
  orderId: '',
})

const selectAssetInfo = ref({
  visible: false,
  row: {},
  query: {
    queryMap: {},
  },
  checkedList: [],
})

function returnModify() {
  if (modifyInfo.value.orderId) {
    getList()
  } else {
    search()
  }
}
onMounted(() => {
  filterInfo.value.copyQuery = proxy.$fn.deepClone(filterInfo.value.query)
})
function timeChange(val) {
  if (val && val.length) {
    filterInfo.value.query.startDate = val[0]
    filterInfo.value.query.endDate = val[1]
  } else {
    filterInfo.value.query.startDate = ''
    filterInfo.value.query.endDate = ''
  }
}
//创建
function create() {
  modifyInfo.value.orderId = ''
  modifyInfo.value.title = '新增招标项目'
  modifyInfo.value.visible = true
}
function update(row) {
  sessionStorage.setItem('storeRowId', row.id)
  modifyInfo.value.orderId = row.id
  modifyInfo.value.title = '修改招标项目'
  modifyInfo.value.visible = true
}

//展示明细
function showDetail(row) {
  sessionStorage.setItem('storeRowId', row.id)
  detailInfo.value.orderId = row.id
  detailInfo.value.visible = true
}
function tableRowClassName({ row }) {
  let rowId = sessionStorage.getItem('storeRowId')
  if (rowId && row.id === Number(rowId)) {
    return 'check-row'
  }
  return ''
}
function mappingAsset(row) {
  getMappingBiddingProjectAssetById(row.id).then((res) => {
    if (res && res.success) {
      selectAssetInfo.value.visible = true
      selectAssetInfo.value.row = row
      if (res.data && res.data.length) {
        let ids = res.data.map((item) => item.assetId)
        selectAssetInfo.value.checkedList = ids
      } else {
        selectAssetInfo.value.checkedList = []
      }
    }
  })
}
function callBackAsset(data) {
  let ids = []
  if (data && data.length) {
    ids = data.map((item) => {
      return {
        assetId: item.id,
        biddingProjectId: selectAssetInfo.value.row.id,
      }
    })
  }
  let params = {
    ...selectAssetInfo.value.row,
    mappingAssetDetails: ids,
  }
  mappingBiddingProjectAssetMappingAsset(params).then((res) => {
    if (res && res.success) {
    }
  })
}
function remove(row) {
  delProject([row.id]).then((res) => {
    if (res && res.success) {
      getList()
    }
  })
}
function clearSearch() {
  clearType.value = !clearType.value
  filterInfo.value.query = proxy.$fn.deepClone(filterInfo.value.copyQuery)
  openTime.value = []
  search()
}
function getList() {
  tableInfo.value.api = getProjectPage
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.api = getProjectPage
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
getList()
</script>

<style scoped></style>
